 var socket = io.connect('http://localhost');
  //链接成功
  socket.on('connect', function () {
	  send('login',{id:userid,username:username,realname:realname});
  });
  //收到消息
  socket.on('message', function (msg) {
	 var m=JSON.parse(msg);
	 if(m && m.type){
		 switch(m.type){
		   case 'online':
			   if(m.msg){
				   $('#onlineC').append('<tr id="'+m.msg.id+'"><a href=""><td><i class="icon-user"></i>'+m.msg.realname+'</td></a></tr>');
			   }else{
				   $('#onlineC').append('<tr id="'+m.msg.id+'"><a href=""><td><i class="icon-user"></i>未知</td></a></tr>');
			   }
			   $('#ccount').html(parseInt($('#ccount').html())+1);
			   break;
		   case 'offline':
			   $('#'+m.msg.id).remove();
			   $('#ccount').html(parseInt($('#ccount').html())-1);
			   break;
		   case 'allonline':
			   if(m.msg){
				   if(m.msg.length){
					   $('#onlineC').html('<tr><td>在线客服<span id="ccount">'+(m.msg.length+1)+'</span>人</td></tr>');
				   }else{
					   $('#onlineC').html('<tr><td>在线客服<span id="ccount">1</span>人</td></tr>');
				   }
				   for(var i=0;i<m.msg.length;i++){
					   var us=m.msg[i];
					   $('#onlineC').append('<tr id="'+us.id+'"><a href=""><td><i class="icon-user"></i>'+us.realname+'</td></a></tr>');
				   }
			   }else{
				   $('#onlineC').html('<tr><td>在线客服<span id="ccount">1</span>人</td></tr>');
			   }
			   break;
		 }
	 } 
  });
  //发送消息
  function send(type,ms){
	  var msg={};
	  msg.type=type;
	  msg.msg=ms;
	  socket.send(JSON.stringify(msg));
  }
	var start=false;
	var recorder;
	var onFail = function(e) {
		console.log('Rejected!', e);
	};
	var onSuccess = function(s) {
		var context = new webkitAudioContext();
		var mediaStreamSource = context.createMediaStreamSource(s);
		recorder = new Recorder(mediaStreamSource);
		recorder.record();
	}
	window.URL = window.URL || window.webkitURL;
	navigator.getUserMedia  = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
	
	function startRecording() {
		if(start){
			start=false;
			$('#bt').val("start");
			recorder.stop();
			recorder.exportWAV(function(s) {
				var s = window.URL.createObjectURL(s);
				socket.send(s);
			});
			
		}else{
			start=true;
			$('#bt').val("send");
			if (navigator.getUserMedia) {
				navigator.getUserMedia({audio: true}, onSuccess, onFail);
			} else {
				console.log('navigator.getUserMedia not present');
			}
		}
	}
